package com.xuz;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @user 65453
 * @date 2024/12/3 21:28
 */
public class RawHTTPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket socketServer = new ServerSocket(8080);

        while (true) {
            Socket socket = socketServer.accept();
            System.out.println("A socket created");

            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuilder requestBuilder = new StringBuilder();
            String line = "";

            while (!(line = bufferedReader.readLine()).isEmpty()) {
                requestBuilder.append(line + "\n");
            }
            String request = requestBuilder.toString();
            System.out.println(request);

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedWriter.write("HTTP/1.1 200 ok\n\nHello World!\n");
            bufferedWriter.flush();
            socket.close();


        }
    }
}
